﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_SignInDoc]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_SignInDoc];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_SignInDoc]
    @StaffName nvarchar(300),
    @DocID int
AS
SET NOCOUNT ON
DECLARE @staff_id int
DECLARE @PassNum int

SELECT     @staff_id = staff_id 
    FROM 
        uds_staff
    WHERE     staff_name = @StaffName

IF @staff_id>0
BEGIN

    --得到汇签规则
    SELECT     @PassNum = e.PassNum 
        FROM 
            uds_flow_status c,
            uds_flow_document d,
            uds_flow_step e
        WHERE         c.doc_id         = d.doc_id 
              and e.flow_id         = d.flow_id 
              and e.step_id         = d.step_id 
              and c.doc_id         = @docid

    
    IF @PassNum = 0
    BEGIN
    --没有汇签,更新文档到签收待批状态
        UPDATE     uds_flow_document 
            SET doc_status =1
            WHERE doc_id = @DocID 
    END
    ELSE

    BEGIN
    --多人汇签，更新文档到会签状态
        UPDATE     uds_flow_document 
            SET doc_status     = 2
            WHERE doc_id     = @DocID 
    END
    --刷新对文档的状态
    UPDATE     uds_flow_status 
        SET     status =1 
        WHERE         doc_id     = @DocID 
              and staff_id     = @staff_id
    
    RETURN 0        
END
ELSE
    RETURN -1
SET NOCOUNT OFF